package com.photoeditor.demo.ui.widget.image.edit;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.android.gpuimage.AdjustGPUImageView;
import com.android.gpuimage.GPUImageAdjustFilter;
import com.photoeditor.R;
import com.photoeditor.demo.model.image.util.ImageHelper;
import com.photoeditor.demo.statistics.StatisticsConstant;
import com.photoeditor.demo.statistics.StatisticsUtils;
import com.photoeditor.demo.ui.activity.edit.EditorImageActivity;
import com.photoeditor.demo.ui.widget.image.CustomTabCheckBg;
import com.photoeditor.demo.ui.widget.image.IOnProgressChange;


public class AdjustBarView extends LinearLayout implements IDealInit, IOnProgressChange {

    private LinearLayout mMainLayout;

    private CustomTabCheckBg mContrastBtn;
    private CustomTabCheckBg mSaturation;
    private CustomTabCheckBg mBrightness;
    private CustomTabCheckBg mVignette;
    private CustomTabCheckBg mSharpenBtn;
    //	private CustomTabButton mGrainBtn;
    private CustomTabCheckBg mTemperatureBtn;
    private CustomTabCheckBg mToneBtn;

    private int mCurId;

    private AdjustGPUImageView mAdjustGPUImageView;

    private GPUImageAdjustFilter mAdjustFilter;

    /**
     * 用于存储当前的各个Filter的值
     */
    private int mContrastProgress = 50;
    private int mSaturationProgress = 50;
    private int mBrightnessProgress = 50;
    private int mVignetteProgress = 0;

    private int mSharpenProgress = 0;
    //	private int mGrainProgress = 0;
    private int mTemperatureProgress = 50;
    private int mToneProgress = 50;

    private boolean mModified = false;

    private EditorImageActivity mActivity;

    public AdjustBarView(Context context) {
        this(context, null);
    }

    public AdjustBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
        mActivity = (EditorImageActivity) context;
    }

    private void initialize() {
        mAdjustFilter = new GPUImageAdjustFilter();
    }

    @Override
    public void init() {
        mMainLayout = (LinearLayout) findViewById(R.id.main_layout);

        mContrastBtn = (CustomTabCheckBg) findViewById(R.id.contrast);
        mSaturation = (CustomTabCheckBg) findViewById(R.id.saturation);
        mBrightness = (CustomTabCheckBg) findViewById(R.id.brightness);
        mVignette = (CustomTabCheckBg) findViewById(R.id.vignette);
        mSharpenBtn = (CustomTabCheckBg) findViewById(R.id.sharpen);
//		mGrainBtn = (CheckableImageView) findViewById(R.id.grain);
        mTemperatureBtn = (CustomTabCheckBg) findViewById(R.id.temperature);
        mToneBtn = (CustomTabCheckBg) findViewById(R.id.tone);

        int width = (int) (ImageHelper.SCREEN_WIDTH / 5.58f);
        int count = mMainLayout.getChildCount();
        for (int i = 0; i < count; i++) {
            View v = mMainLayout.getChildAt(i);
            LayoutParams lp = (LayoutParams) v.getLayoutParams();
            lp.width = width;
            v.setLayoutParams(lp);
        }

        initView();
        initResource();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();

    }

    private void initView() {
        OnClickListener clickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                int id = v.getId();
                mCurId = id;
                mActivity.setConfirmEnable(true);
                showSeekbarById(mCurId);
                checkBtn(mCurId);
            }
        };
        mSaturation.setOnClickListener(clickListener);
        mContrastBtn.setOnClickListener(clickListener);
        mBrightness.setOnClickListener(clickListener);
        mVignette.setOnClickListener(clickListener);

        mSharpenBtn.setOnClickListener(clickListener);
        mTemperatureBtn.setOnClickListener(clickListener);
        mToneBtn.setOnClickListener(clickListener);
    }


    private void showSeekbarById(int id) {
        if (id == R.id.contrast) {
            mActivity.showInsideBottomBarWithProgress(mContrastProgress);
        } else if (id == R.id.saturation) {
            mActivity.showInsideBottomBarWithProgress(mSaturationProgress);
        } else if (id == R.id.brightness) {
            mActivity.showInsideBottomBarWithProgress(mBrightnessProgress);
        } else if (id == R.id.vignette) {
            mActivity.showInsideBottomBarWithProgress(mVignetteProgress);
        } else if (id == R.id.sharpen) {
            mActivity.showInsideBottomBarWithProgress(mSharpenProgress);
        } else if (id == R.id.temperature) {
            mActivity.showInsideBottomBarWithProgress(mTemperatureProgress);
        } else if (id == R.id.tone) {
            mActivity.showInsideBottomBarWithProgress(mToneProgress);
        }
    }

    private void checkBtn(int id) {
        if (id == R.id.contrast) {
            mContrastBtn.setChecked(true);
            mSaturation.setChecked(false);
            mBrightness.setChecked(false);
            mVignette.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(false);

            mToneBtn.setChecked(false);
            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_CONTRAST_CLICK);
        } else if (id == R.id.saturation) {
            mSaturation.setChecked(true);
            mContrastBtn.setChecked(false);
            mBrightness.setChecked(false);
            mVignette.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(false);

            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_SATURATION_CLICK);
        } else if (id == R.id.brightness) {
            mBrightness.setChecked(true);
            mContrastBtn.setChecked(false);
            mSaturation.setChecked(false);
            mVignette.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(false);

            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_BRIGHTNESS_CLICK);
        } else if (id == R.id.vignette) {
            mVignette.setChecked(true);
            mBrightness.setChecked(false);
            mContrastBtn.setChecked(false);
            mSaturation.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(false);

            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_VIGNETTE_CLICK);
        } else if (id == R.id.sharpen) {
            mVignette.setChecked(false);
            mBrightness.setChecked(false);
            mContrastBtn.setChecked(false);
            mSaturation.setChecked(false);

            mSharpenBtn.setChecked(true);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(false);

            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_SHARPEN_CLICK);
        } else if (id == R.id.temperature) {
            mVignette.setChecked(false);
            mBrightness.setChecked(false);
            mContrastBtn.setChecked(false);
            mSaturation.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(true);
            mToneBtn.setChecked(false);

            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_TEMPERATURE_CLICK);
        } else if (id == R.id.tone) {
            mVignette.setChecked(false);
            mBrightness.setChecked(false);
            mContrastBtn.setChecked(false);
            mSaturation.setChecked(false);

            mSharpenBtn.setChecked(false);
//			mGrainBtn.setChecked(false);
            mTemperatureBtn.setChecked(false);
            mToneBtn.setChecked(true);
            StatisticsUtils.statisticsCustomPreviewClick(StatisticsConstant.UMENG_CUSTOM_EVENT_FUNC_PREVIEW_VALUE_EDITOR_PARAM_TONE_CLICK);
        }
    }

    /**
     * 恢复默认状态
     */
    public boolean cancelFilter() {
        boolean consume = false;

        if (mCurId == R.id.contrast) {
            consume = true;
            mContrastProgress = 50;
            mAdjustFilter.setContrast(range(mContrastProgress, 0.4f, 1.6f));
            mContrastBtn.setChecked(false);
        } else if (mCurId == R.id.saturation) {
            consume = true;
            mSaturationProgress = 50;
            mAdjustFilter.setSaturation(range(mSaturationProgress, 0.0f, 2.0f));
            mSaturation.setChecked(false);
        } else if (mCurId == R.id.brightness) {
            consume = true;
            mBrightnessProgress = 50;
            mAdjustFilter.setBrightness(range(mBrightnessProgress, -0.5f, 0.5f));
            mBrightness.setChecked(false);
        } else if (mCurId == R.id.vignette) {
            consume = true;
            mVignetteProgress = 0;
            mAdjustFilter.setVignetteStart(range(mVignetteProgress, 0.7f, 0f));
            mVignette.setChecked(false);
        } else if (mCurId == R.id.sharpen) {
            consume = true;
            mSharpenProgress = 0;
            mAdjustFilter.setSharpness(range(mSharpenProgress, 0f, 1.5f));
            mSharpenBtn.setChecked(false);
        }
//		else if(mCurId == R.id.grain){
//			mActivity.showInsideBottomBarWithProgress(mGrainProgress);
        //		mGrainProgress = 0;
        //		mGrainBtn.setChecked(false);
//		}
        else if (mCurId == R.id.temperature) {
            consume = true;
            mTemperatureProgress = 50;
            mAdjustFilter.setTemperature(range(mTemperatureProgress, 4000.0f, 6000.0f));
            mTemperatureBtn.setChecked(false);
        } else if (mCurId == R.id.tone) {
            consume = true;
            mToneProgress = 50;
            mAdjustFilter.setCurve(range(mToneProgress, -0.15f, 0.45f));
            mToneBtn.setChecked(false);
        } else {
            reset();
        }
        mAdjustGPUImageView.requestRender();
        mCurId = View.NO_ID;

        return consume;
    }

    private void reset() {
        mContrastProgress = 50;
        mSaturationProgress = 50;
        mBrightnessProgress = 50;
        mVignetteProgress = 0;

        mSharpenProgress = 0;
//		mGrainProgress = 0;
        mTemperatureProgress = 50;
        mToneProgress = 50;

        mAdjustFilter.setContrast(range(mContrastProgress, 0.4f, 1.6f));
        mAdjustFilter.setSaturation(range(mSaturationProgress, 0.0f, 2.0f));
        mAdjustFilter.setBrightness(range(mBrightnessProgress, -0.5f, 0.5f));
        mAdjustFilter.setVignetteStart(range(mVignetteProgress, 0.7f, 0f));

        mAdjustFilter.setSharpness(range(mSharpenProgress, 0f, 1.5f));
        mAdjustFilter.setTemperature(range(mTemperatureProgress, 4000.0f, 6000.0f));
        mAdjustFilter.setCurve(range(mToneProgress, -0.15f, 0.45f));

        mAdjustGPUImageView.requestRender();

        mContrastBtn.setChecked(false);
        mSaturation.setChecked(false);
        mBrightness.setChecked(false);
        mVignette.setChecked(false);
        mSharpenBtn.setChecked(false);
//		mGrainBtn.setChecked(false);
        mTemperatureBtn.setChecked(false);
        mToneBtn.setChecked(false);
    }

    public boolean onConfirmClick() {
        boolean consume = false;
        if (mCurId == R.id.contrast) {
            consume = true;
            setModified();
        } else if (mCurId == R.id.saturation) {
            consume = true;
            setModified();
        } else if (mCurId == R.id.brightness) {
            consume = true;
            setModified();
        } else if (mCurId == R.id.vignette) {
            consume = true;
            setModified();
        } else if (mCurId == R.id.sharpen) {
            consume = true;
            setModified();
        }
//		else if(mCurId == R.id.grain){
//			mActivity.showInsideBottomBarWithProgress(mGrainProgress);
//		}
        else if (mCurId == R.id.temperature) {
            consume = true;
            setModified();
        } else if (mCurId == R.id.tone) {
            consume = true;
            setModified();
        }
        mCurId = View.NO_ID;
        return consume;
    }

    public boolean isModified() {
        return mModified;
    }

    public void setModified() {
        if (!mModified) {
            mModified = true;
        }
    }

    public AdjustGPUImageView getAdjustGPUImageView() {
        return mAdjustGPUImageView;
    }

    public void setmAdjustGPUImageView(AdjustGPUImageView mAdjustGPUImageView) {
        this.mAdjustGPUImageView = mAdjustGPUImageView;
        mAdjustGPUImageView.setFilter(mAdjustFilter);
    }

    protected float range(final int percentage, final float start, final float end) {
        return (end - start) * percentage / 100.0f + start;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        if (visibility == View.VISIBLE && mAdjustGPUImageView != null) {
            mAdjustGPUImageView.setFilter(mAdjustFilter);
        }
    }

    public void onDestroy() {
    }

    @Override
    public void onProgressChange(int progress) {
        if (mCurId == R.id.contrast) {
            mContrastProgress = progress;
            mAdjustFilter.setContrast(range(progress, 0.4f, 1.6f));
            mAdjustGPUImageView.requestRender();
        } else if (mCurId == R.id.saturation) {
            mSaturationProgress = progress;
            mAdjustFilter.setSaturation(range(progress, 0.0f, 2.0f));
            mAdjustGPUImageView.requestRender();
        } else if (mCurId == R.id.brightness) {
            mBrightnessProgress = progress;
            mAdjustFilter.setBrightness(range(progress, -0.5f, 0.5f));
            mAdjustGPUImageView.requestRender();

        } else if (mCurId == R.id.vignette) {
            mVignetteProgress = progress;
            mAdjustFilter.setVignetteStart(range(progress, 0.7f, 0f));
            mAdjustGPUImageView.requestRender();
        } else if (mCurId == R.id.sharpen) {
            mSharpenProgress = progress;
            mAdjustFilter.setSharpness(range(mSharpenProgress, 0f, 1.5f));
            mAdjustGPUImageView.requestRender();
        }
//		else if(mCurId == R.id.grain){
//			mGrainProgress = progress;
//			//TODO
//		}
        else if (mCurId == R.id.temperature) {
            mTemperatureProgress = progress;
            mAdjustFilter.setTemperature(range(mTemperatureProgress, 4000.0f, 6000.0f));
            mAdjustGPUImageView.requestRender();
        } else if (mCurId == R.id.tone) {
            mToneProgress = progress;
            mAdjustFilter.setCurve(range(mToneProgress, -0.15f, 0.45f));
            mAdjustGPUImageView.requestRender();
        }
    }


    private void initResource() {
        setBackgroundResource(R.drawable.image_edit_sencond_bg);

//		mContrastBtn.setThemeImageRes(R.drawable.image_edit_adjust_contrast, View.NO_ID);
//		mSaturation.setThemeImageRes(R.drawable.image_edit_adjust_saturation, View.NO_ID);
//		mBrightness.setThemeImageRes(R.drawable.image_edit_adjust_brightness, View.NO_ID);
//		mVignette.setThemeImageRes(R.drawable.image_edit_adjust_vignette, View.NO_ID);
//		mSharpenBtn.setThemeImageRes(R.drawable.image_edit_adjust_sharpen, View.NO_ID);
//		//mGrainBtn.setThemeImageRes(R.drawable.image_edit_adjust_contrast, View.NO_ID);
//		mTemperatureBtn.setThemeImageRes(R.drawable.image_edit_adjust_temperature, View.NO_ID);
//		mToneBtn.setThemeImageRes(R.drawable.image_edit_adjust_tone, View.NO_ID);
    }
}
